<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class userAction extends commonAction
{  
    public function userList(){
        $whereMap=array(
            'role'=>'普通用户',
            'state'=>'active'
        );
        if($_POST['user-id']){
            $whereMap['id']=$_POST['user-id'];
        }
        if($_POST['username']){
            $whereMap['username']=$_POST['username'];
        }
        $count = M('user')->where("state='active'")->count();
        list($pagesize, $offset, $pagestring) = pagestring($count, 5);
        $this->userList=D('user')->where($whereMap)->limit($offset, $pagesize)->order('register_time desc')->select();
        $this->assign('pagestring', $pagestring);
        $this->display();
    }
    public function userInfo(){
        $whereMap=array(
            //'role'=>'普通用户',
            'state'=>'active'
        );
        if($_GET['id']){
            $whereMap['id']=$_GET['id'];
        }
        $this->userInfo=D('user')->where($whereMap)->find();
        $this->display();
    }
    public function userEdit(){
        $id=$_POST['id'];
        unset($_POST['id']);
        $data=$_POST;
        $res=M('user')->where("id=$id")->data($data)->save();//print_r($res);exit();
        if($res){
            redirect("userInfo?id=$id");
        }else{
            redirect("userInfo?id=$id&flag=error");
        }
    }
    public function findUserByField(){
        $whereMap=array(
            'role'=>'普通用户',
            'state'=>'active'
        );
        if($_POST['user_id']){
            $whereMap['id']=$_POST['user_id'];
        }
        if($_POST['username']){
           $whereMap['username']=$_POST['username'];          
        }
        $userList=D('user')->where($whereMap)->find();//print_r($userList);exit();
        if($userList){
            $this->assign('id',$userList['id']);
            $this->assign('username',$userList['username']);
            $this->display('userDrop');
        }else{
            $this->assign('error','该用户不存在');
            $this->display('userDrop');
        }
    }
    public function userDrop(){
        $whereMap=array(
            'state'=>'active',
            'role'=>'普通用户',
        );
        if($_GET['id']){
            $whereMap['id']=$_GET['id'];
            $this->assign('id',$_GET['id']);
        }
        if($_GET['username']){
           $whereMap['username']=$_GET['username']; 
           $this->assign('username',$_GET['username']);
        } 
        $this->display();
    }
    public function confirmDrop(){
       $whereMap=array();
       if($_POST['user-id']&&$_POST['username']){
            $whereMap['id']=$_POST['user-id'];
            $whereMap['username']=$_POST['username']; 
        }else{
            $this->assign('error','用户ID和用户名不能为空');
            $this->display('userDrop');
            exit();
        }
        $res=D('user')->where($whereMap)->setField('state','disable');
        if($res){
            $this->assign('success','删除用户成功');
        }else{
            $this->assign('error','删除用户失败');
        }
        $this->display('userDrop');
    }
    public function userOnline(){
        $whereMap=array(
            'state'=>'active',
            'role'=>'普通用户',
            'is_online'=>'online'
        );
        if($_POST['user-id']){
            $whereMap['id']=$_POST['user-id'];
        }
        if($_POST['username']){
            $whereMap['username']=$_POST['username'];
        }
        $count = M('user')->where($whereMap)->count();
        list($pagesize, $offset, $pagestring) = pagestring($count, 5);
        $this->userList=D('user')->where($whereMap)->limit($offset,$pagesize)->select();
        $this->assign('pagestring', $pagestring);
        $this->display();
    }
    public function userResetPwd(){
        $this->assign('username',$_SESSION['userInfo']['username']);
        $whereMap=array(
            'state'=>'active',
        );
        $data=array();
        if($_POST['username']){
            $whereMap['username']=$_POST['username'];
        }
        if($_POST['password']){
            $data['password']=md5($_POST['password']);
        }
        $res=D('user')->data($data)->where($whereMap)->save();
        if($res){
            $this->assign('success','success');
        }
        $this->display();
    }
}
?>
